<?php
/**
 * 缓存相关函数 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: func_cache.php 442 2010-05-26 07:14:01Z lisijie86 $
*/

!defined('IN_JBLOG') && exit('Access Denied!');

/**
 * 刷新系统缓存
 * 
 * @param string $cachestr 刷新列表，用英文逗号","分割，如果值为空则清空所有缓存
 */
function recache($cachestr = '') {
	global $cache;
	if (!$cachestr) {
		$cache->clear();
	} else {
		$cachelist = explode(',',$cachestr);
		foreach ($cachelist as $name) {
			$name = "{$name}_recache";
			function_exists($name) && $name();
		}
	}
}

//缓存配置信息
function config_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('config')." WHERE `type` NOT IN ('usergroup','widget','mail','blogmeta','pagemeta')";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$content[$row['name']] = $row['value'];
	}
	$cache->set('config', $content);
}

//缓存邮件设置信息
function mail_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('config')." WHERE `type` = 'mail'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$content[$row['name']] = $row['value'];
	}
	$cache->set('mail', $content);	
}

//缓存用户组
function usergroup_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT `name`,`value` FROM ".tname('config')." WHERE `type` = 'usergroup'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$row['name'] = str_replace('usergroup_','',$row['name']);
		$content[$row['name']] = unserialize($row['value']);
	}
	$cache->set('usergroup', $content);	
}

//缓存Weigets设置信息
function widget_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('config')." WHERE `type` = 'widget'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$content[$row['name']] = unserialize($row['value']);
	}
	$cache->set('widget', $content);	
}

//缓存blog自定义域信息
function blogmeta_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('config')." WHERE `type` = 'blogmeta'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$row['value'] = unserialize($row['value']);
		$row['value']['name'] = $row['name'];
		$content[$row['name']] = $row['value'];
	}
	$cache->set('blogmeta', $content);	
}

//缓存页面自定义域信息
function pagemeta_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('config')." WHERE `type` = 'pagemeta'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$row['value'] = unserialize($row['value']);
		$row['value']['name'] = $row['name'];
		$content[$row['name']] = $row['value'];
	}
	$cache->set('pagemeta', $content);	
}

//缓存分类
function cate_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT * FROM ".tname('cate')." ORDER BY `type`, orderid";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$query2 = $db->query("SELECT `var`, `value` FROM ".tname('meta')." WHERE `table` = 'cate' AND dataid = '$row[id]'");
		while ( $meta = $db->fetch_array($query2) ) {
			$row[$meta['var']] = $meta['value'];
		}
		$content[$row['type']][]=$row['id'];
		$content[$row['id']] = $row;
	}
	$cache->set('cate', $content);
}

//缓存友情链接
function link_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT l.*, c.catename FROM ".tname('link')." l LEFT JOIN ".tname('cate')." c 
	ON l.cateid = c.id WHERE l.`status` = '1' ORDER BY c.orderid, l.orderid";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		if ( !array_key_exists($row['cateid'], $content) ) {
			$content[$row['cateid']] = array(
				'catename' => $row['catename'],
				'links' => array()
			);
		}
		$content[$row['cateid']]['links'][] = array(
			'name' => $row['name'],
			'url' => $row['url'],
			'logo' => $row['logo'],
			'description' => $row['description'],
		);
	}
	$cache->set('link', $content);
}

//缓存Tag标签
function tag_recache() {
	global $db, $cache;
	$content = array();
	$sql = "SELECT id,tagname,`count` FROM ".tname('tag')." WHERE `count` > 0 ORDER BY id ASC";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$content[$row['id']] = array('tagname' => $row['tagname'],'count' => $row['count']);
	}
	$cache->set('tag', $content);
}
?>